/*
 * jpBuscaLista.java
 *
 * Created on 14 de septiembre de 2007, 10:51
 */

package legajopersonal;
import utiles.*;
import java.sql.*;
import Cliente.*;
import javax.swing.*;
import java.util.ArrayList;
/**
 *
 * @author  practicante
 */
public class jPBuscar extends jPTabla{
    
    /** Creates new form jpBuscaLista */
    public jPBuscar() {
        initComponents();     
        this.EstablecerConexionRMI();
        this.rbtnPorCodigoBuscar.setSelected(true);
        this.rbtnPorCodigoBuscar.requestFocus();    
        this.btnAceptar.setEnabled(false);
        this.Nemonics();        
    }
       
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        btnGroupBuscar = new javax.swing.ButtonGroup();
        rbtnPorCodigoBuscar = new javax.swing.JRadioButton();
        rbtnPorNombreBuscar = new javax.swing.JRadioButton();
        jtfEntradaBuscar = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnBuscar = new javax.swing.JButton();
        jlblTitulo = new javax.swing.JLabel();
        btnSalir = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();
        jlblTrabajador = new javax.swing.JLabel();

        rbtnPorCodigoBuscar.setFont(new java.awt.Font("Tahoma", 1, 11));
        rbtnPorCodigoBuscar.setText("Por C\u00f3digo");
        rbtnPorCodigoBuscar.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbtnPorCodigoBuscar.setMargin(new java.awt.Insets(0, 0, 0, 0));
        rbtnPorCodigoBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnPorCodigoBuscarActionPerformed(evt);
            }
        });
        rbtnPorCodigoBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                rbtnPorCodigoBuscarKeyPressed(evt);
            }
        });

        rbtnPorNombreBuscar.setFont(new java.awt.Font("Tahoma", 1, 11));
        rbtnPorNombreBuscar.setText("Por Nombre");
        rbtnPorNombreBuscar.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbtnPorNombreBuscar.setMargin(new java.awt.Insets(0, 0, 0, 0));
        this.btnGroupBuscar.add(this.rbtnPorCodigoBuscar);
        this.btnGroupBuscar.add(this.rbtnPorNombreBuscar);
        rbtnPorNombreBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnPorNombreBuscarActionPerformed(evt);
            }
        });
        rbtnPorNombreBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                rbtnPorNombreBuscarKeyPressed(evt);
            }
        });

        jtfEntradaBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfEntradaBuscarActionPerformed(evt);
            }
        });
        jtfEntradaBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfEntradaBuscarKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtfEntradaBuscarKeyReleased(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("C\u00f3digo");

        btnBuscar.setFont(new java.awt.Font("Arial", 1, 14));
        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/find.gif")));
        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        btnBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnBuscarKeyPressed(evt);
            }
        });

        jlblTitulo.setFont(new java.awt.Font("Tahoma", 1, 11));
        jlblTitulo.setText("BUSCAR TRABAJADOR");

        btnSalir.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/closeall.gif")));
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/options.gif")));
        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rbtnPorCodigoBuscar)
                        .addGap(53, 53, 53)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(rbtnPorNombreBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAceptar)
                        .addGap(19, 19, 19)
                        .addComponent(btnSalir))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jlblTrabajador, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(jtfEntradaBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(30, 30, 30)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(225, Short.MAX_VALUE)
                .addComponent(jlblTitulo)
                .addGap(218, 218, 218))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlblTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rbtnPorCodigoBuscar)
                        .addGap(23, 23, 23)
                        .addComponent(rbtnPorNombreBuscar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfEntradaBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnBuscar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jlblTrabajador, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnAceptar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
// TODO add your handling code here:
        this.AceptarBuscar();
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
// TODO add your handling code here:
        /*this.CerrarDialogo();
        this.botonSalir();
        this.obtenerPrincipal().HabilitarMenuBar();*/
        this.cerrarPadre();   
        if(this.datos == true){
            this.Datos.Cancelar();
        }
        if(this.maestro == true){
            this.MaestroConsulta.Cancelar();
        }
        if(this.terceros == true){
            this.Perterceros.Cancelar();
        }
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnBuscarKeyPressed
// TODO add your handling code here:
         if(evt.getKeyCode()== evt.VK_ENTER){
            this.ConsultarTrabajador();
            this.btnAceptar.requestFocus();
         }
    }//GEN-LAST:event_btnBuscarKeyPressed

    private void rbtnPorNombreBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_rbtnPorNombreBuscarKeyPressed
// TODO add your handling code here:
         if(evt.getKeyCode()== evt.VK_ENTER){
            this.jtfEntradaBuscar.requestFocus();
         }

    }//GEN-LAST:event_rbtnPorNombreBuscarKeyPressed

    private void rbtnPorCodigoBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_rbtnPorCodigoBuscarKeyPressed
// TODO add your handling code here:
         if(evt.getKeyCode()== evt.VK_ENTER){
            this.rbtnPorNombreBuscar.requestFocus();
         }

    }//GEN-LAST:event_rbtnPorCodigoBuscarKeyPressed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
// TODO add your handling code here:
        this.ConsultarTrabajador();
    }//GEN-LAST:event_btnBuscarActionPerformed
    public void ConsultarTrabajador(){    
    String consulta;    
    int posBus = -1;
        if( this.rbtnPorCodigoBuscar.isSelected() ){
            trab = this.jtfEntradaBuscar.getText().trim();
            this.posSelCod = this.getUtilBus().buscaClave( this.jtfEntradaBuscar.getText().trim() );
        }else if( this.rbtnPorNombreBuscar.isSelected() ){
            trab = this.jtfEntradaBuscar.getText().trim();
            this.posSelCod = this.getUtilBus().getIndice();
        }
        if( this.posSelCod == -1 ){
            Utiles_MensajesDialogo.mensajeAlerta( "No se encuenta el Trabajador" );                      
            this.btnAceptar.setEnabled(false);
            this.btnSalir.setEnabled(true);
            //this.inicializarTabla(cabe,this.tabla);    
            this.btnAceptar.setEnabled(false);
        }else{            
            this.cod = this.getUtilBus().getSoloClave( this.posSelCod ).toString();
            this.DNI = cod;
            System.out.println("que es eso:"+cod);            
            consulta = "SELECT (rtrim(P.Ap_paterno)+' '+ rtrim(P.Ap_materno)+' ' +rtrim(P.Nombre)) " +
                            "FROM trabajador T INNER JOIN Persona P ON T.DNI = P.codigo_persona " +
                            "where P.codigo_persona ='"+cod+"'";
            System.out.println("Consulta:"+consulta);
            //Obtener Datos del trabajador
            if(this.rbtnPorCodigoBuscar.isSelected()){
                try{
                    trab = this.getCliRMI().getObjetoCLIRMI().getCodigo(consulta,this.getID());
                }catch(Exception e){}
                this.jlblTrabajador.setText(trab);            
            }
            this.btnAceptar.setEnabled(true);
        }
    }
    public void AceptarBuscar(){
        try{
        if(this.DNI.equals("")){
            javax.swing.JOptionPane.showMessageDialog(this,"No se ha elegido un trabajador");
        }else{            
            this.cerrarPadre();
            if(this.datos == true){
                this.Datos.SetBotones(true);
                this.Datos.setDNI(this.DNI);
                if(this.Datos.eliminar==true){
                   this.Datos.asignarTrabEliminar(DNI);            
                }    
                else{
                    this.Datos.asignarTrabActualizar(DNI);
                }
            }
            if(this.maestro == true){
                this.MaestroConsulta.setDNI(DNI);                
                if(this.MaestroConsulta.eliminar==true){
                   this.MaestroConsulta.asignarTrabEliminar(DNI);            
                } 
                else{
                    this.MaestroConsulta.asignarTrabActualizar(DNI);
                }
            }   
            if(this.terceros == true){
                this.Perterceros.setDNI(DNI);            
                if(this.Perterceros.eliminar==true){
                   this.Perterceros.asignarTrabEliminar(DNI);            
                } 
                else{
                    this.Perterceros.asignarTrabActualizar(DNI);
                }
            }  
            if(this.consasist == true){
                this.getPanelConsultaAsist().setDNI(DNI);
                System.out.println(trab);
                this.getPanelConsultaAsist().setTrabajador(this.trab);                
            }
        }
        }catch(Exception e){
            e.getStackTrace();
        }
    }
    public String Obtenerconsulta(){
        return this.consultaActiva;
    }
    public String ObtenerDNI(){
        return DNI;
    }   
    public void setTitulo(String titulo){
        this.jlblTitulo.setText(titulo);
        
    }   
    public void setDatos(){
        maestro = false;
        datos = true;
        terceros = false;
    }
    public void setConsultaAsistencia(){
        consasist = true;
        maestro = false;
        datos = false;
        terceros = false;
    }
    public void setPerterceros(){
        terceros = true;
        maestro = false;
        datos = false;
    }
    public void setMaestro(){
        terceros = false;
        maestro = true;
        datos = false;
    }
    public void setFlagMaestro(){
        maestro = true;        
    }
    public void Nemonics(){
        this.rbtnPorCodigoBuscar.setMnemonic('C');
        this.rbtnPorNombreBuscar.setMnemonic('N');
    }
    private void jtfEntradaBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfEntradaBuscarKeyReleased
// TODO add your handling code here:        
        this.btnAceptar.setEnabled(false);
    }//GEN-LAST:event_jtfEntradaBuscarKeyReleased

    private void jtfEntradaBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfEntradaBuscarKeyPressed
// TODO add your handling code here:
        if(evt.getKeyCode()== evt.VK_ENTER){
            if(this.rbtnPorCodigoBuscar.isSelected()==true){
                this.btnBuscar.requestFocus();
            }
        }
    }//GEN-LAST:event_jtfEntradaBuscarKeyPressed

    private void jtfEntradaBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfEntradaBuscarActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_jtfEntradaBuscarActionPerformed

    private void rbtnPorNombreBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnPorNombreBuscarActionPerformed
// TODO add your handling code here:        
        this.PorCodigo = false;
        this.PorNombre = true;       
        this.getUtilBus().activarBusqueda( true );
    }//GEN-LAST:event_rbtnPorNombreBuscarActionPerformed

    private void rbtnPorCodigoBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnPorCodigoBuscarActionPerformed
// TODO add your handling code here:
        this.PorCodigo = true;
        this.PorNombre = false;
        this.getUtilBus().activarBusqueda( false );
    }//GEN-LAST:event_rbtnPorCodigoBuscarActionPerformed
    /*public void EstablecerBusqueda(){
        String consulta = "select p.codigo_persona, p.nombre, p.Ap_paterno, p.Ap_materno from Persona p, Trabajador t where p.codigo_persona = t.dni";                
        int[] col = { 3, 4, 2 };   
        java.awt.Point punto;
        punto = this.jtfEntradaBuscar.getLocation();
        //Utiles_Busqueda ub = new Utiles_Busqueda( this.getPopupMenu(), this.jtfEntradaBuscar, consulta, col, this.ObtenerViaRemota(),60,10 );
        Utiles_Busqueda ub = new Utiles_Busqueda( this.getPopupMenu(), this.jtfEntradaBuscar, consulta, col, this.getClienteRMI(),punto.x + 205,punto.y + 25,this.getID());
        this.setUtilBus( ub );        
    }*/
    public void setConsulta(String cons){
        this.consultaActiva = cons;        
    }
    public javax.swing.JTextField getCajaTexto(){
        return this.jtfEntradaBuscar;
    }
    public static void main( String[] args ){       
        legajopersonal.jPBuscaLista pan = new jPBuscaLista();
        javax.swing.JFrame jf = Utiles_Panel.creaVentana( pan, "Esto es una prueba" );
        pan.setPadre( 1, jf );
        jf.setVisible( true );        
    }    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnBuscar;
    private javax.swing.ButtonGroup btnGroupBuscar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jlblTitulo;
    private javax.swing.JLabel jlblTrabajador;
    private javax.swing.JTextField jtfEntradaBuscar;
    private javax.swing.JRadioButton rbtnPorCodigoBuscar;
    private javax.swing.JRadioButton rbtnPorNombreBuscar;
    // End of variables declaration//GEN-END:variables
    boolean PorCodigo,PorNombre;    
    javax.swing.DefaultListModel Trabajadores;    
    boolean maestro, terceros = false, datos = false, consasist = false;
    private String cod;
    private int posSelCod;
    ArrayListTableModel arrTab;
    String consultaActiva;
    String cabe[];
    String trab="";
    
}
